package com.huilian.iotbox.data.service.impl;

import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.dao.CommodityPurchaseRestrictionCommonDao;
import com.huilian.iotbox.data.dao.CommodityPurchaseRestrictionRecordCommonDao;
import com.huilian.iotbox.data.dao.OrderItemCommonDao;
import com.huilian.iotbox.data.dto.CommodityPurchaseRestrictionRecordDto;
import com.huilian.iotbox.data.enums.CommodityPurchaseRestrictionRecordStatusEnum;
import com.huilian.iotbox.data.exception.SysException;
import com.huilian.iotbox.data.service.CommodityPurchaseRestrictionRecordCommonService;
import com.huilian.iotbox.data.vo.CommodityPurchaseRestrictionVo;
import com.huilian.iotbox.data.vo.OrderItemVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * @author LinYinDong
 * @date 2024/1/22 16:52
 */
@Service
@Slf4j
public class CommodityPurchaseRestrictionRecordCommonServiceImpl implements CommodityPurchaseRestrictionRecordCommonService {
    @Resource
    private CommodityPurchaseRestrictionCommonDao commodityPurchaseRestrictionCommonDao;
    @Resource
    private CommodityPurchaseRestrictionRecordCommonDao commodityPurchaseRestrictionRecordCommonDao;
    @Resource
    private OrderItemCommonDao orderItemCommonDao;
    @Override
    public ServerResponse addCommodityPurchaseRestrictionRecord(Long orderId) {
        if(StringUtils.isEmpty(orderId)){
            throw new SysException("缺少订单Id");
        }
        CommodityPurchaseRestrictionRecordDto commodityPurchaseRestrictionRecordDto = new CommodityPurchaseRestrictionRecordDto();
        commodityPurchaseRestrictionRecordDto.setStatus(CommodityPurchaseRestrictionRecordStatusEnum.NORMAL.getCode());
        commodityPurchaseRestrictionRecordDto.setOrderId(orderId);
        CommodityPurchaseRestrictionVo commodityPurchaseRestrictionVo = commodityPurchaseRestrictionCommonDao.findOneCommodityPurchaseRestrictionByOrderId(commodityPurchaseRestrictionRecordDto);
        if(!StringUtils.isEmpty(commodityPurchaseRestrictionVo)){
            //查询限购购买套餐数量
            OrderItemVo orderItemVo = orderItemCommonDao.findOrderItemByOrderId(orderId);
            if(StringUtils.isEmpty(orderItemVo.getNum())){
                throw new SysException("添加游戏币套餐限购购买记录失败");
            }
            commodityPurchaseRestrictionRecordDto.setMemberId(commodityPurchaseRestrictionVo.getMemberId());
            commodityPurchaseRestrictionRecordDto.setCommodityPurchaseRestrictionId(commodityPurchaseRestrictionVo.getCommodityPurchaseRestrictionId());
            commodityPurchaseRestrictionRecordDto.setQuantity(orderItemVo.getNum());
            Integer insert = commodityPurchaseRestrictionRecordCommonDao.insert(commodityPurchaseRestrictionRecordDto);
            if(insert <= 0){
                throw new SysException("添加游戏币套餐限购购买记录失败");
            }
        }
        return ServerResponse.createBySuccess();
    }
    /**
     * 修改commodity_purchase_restriction_record的status, 封装过, 只需要传递orderId和status
     */
    @Override
    public ServerResponse updateCommodityPurchaseRestrictionRecordStatusPack(Long orderId,Integer status) {
        CommodityPurchaseRestrictionVo commodityPurchaseRestrictionVo = commodityPurchaseRestrictionCommonDao.findCommodityPurchaseRestrictionByOrderId(orderId);
        if(!ObjectUtils.isEmpty(commodityPurchaseRestrictionVo) && !ObjectUtils.isEmpty(commodityPurchaseRestrictionVo.getCommodityPurchaseRestrictionId())){
            CommodityPurchaseRestrictionRecordDto commodityPurchaseRestrictionRecordDto = new CommodityPurchaseRestrictionRecordDto();
            commodityPurchaseRestrictionRecordDto.setCommodityPurchaseRestrictionId(commodityPurchaseRestrictionVo.getCommodityPurchaseRestrictionId());
            commodityPurchaseRestrictionRecordDto.setStatus(status);
            commodityPurchaseRestrictionRecordDto.setMemberId(commodityPurchaseRestrictionVo.getMemberId());
            Integer result = commodityPurchaseRestrictionRecordCommonDao.updateCommodityPurchaseRestrictionRecordStatus(commodityPurchaseRestrictionRecordDto);
            if(result == 0){
                log.info("修改限购记录失败");
                log.info("orderId:{}，status:{}",orderId,status);
                log.info("commodityPurchaseRestrictionRecordDto: {}",commodityPurchaseRestrictionRecordDto);
                return ServerResponse.createByErrorMessage("修改限购记录失败");
            }
        }
        return ServerResponse.createBySuccess();

    }
}
